package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class SpecParamServiceImpl extends ServiceImpl<SpecParamMapper, SpecParam> implements SpecParamService {
    @Autowired
    SpecParamMapper specParamMapper;
    @Autowired
    SpecGroupService specGroupService;

    @Override
    public List<SpecParamDTO> listSpecParam(Long cid, Long gid, Boolean isSearch) {
        List<SpecParam> specParamList = this.query()
                .eq(cid != null, "category_id", cid)
                .eq(gid != null, "group_id", gid)
                .eq(isSearch != null, "searching", isSearch).list();
        return SpecParamDTO.convertEntityList(specParamList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addSpecParam(SpecParamDTO specParamDTO) {
        this.save(specParamDTO.toEntity(SpecParam.class));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSpecParam(SpecParamDTO specParamDTO) {
        this.updateById(specParamDTO.toEntity(SpecParam.class));
    }

    /*@Override
    @Transactional(rollbackFor = Exception.class)
    public void add(){
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCategoryId(76L);
        specGroup.setName("测试");
        SpecGroupDTO specGroupDTO = new SpecGroupDTO(specGroup);
        for (int i = 0; i < 10; i++) {
            if (i==5){
                int a = 1/0;
            }
            specGroupService.addSpecGroup(specGroupDTO);
        }
    }*/
}